<script lang="ts" setup>
import { onMounted, PropType } from 'vue'
import { Command } from '@/commands/command.ts'
import { copy_to_clipboard } from '@/functions/clipboard.ts'
import { dbus } from '@/dbus/dbus.ts'
import { invoke_proxy } from '@/functions/invoke.ts'
import { BackendResponse } from '@/runtime/response.ts'
import { BACKEND_CODE_OK } from '@/runtime/code.ts'

const props = defineProps({
    command: { required: true, type: Object as PropType<Command> },
})

onMounted(async () => {
    props.command!.execute().finally(async () => {
        dbus.emit('scroll-to-bottom')
    })
})

async function open_link(link: string): Promise<void> {
    const response = await invoke_proxy<BackendResponse<any>>('invoke_open', { path: link })
    if (BACKEND_CODE_OK !== response.code) {
        window.Message.error(response.message)

        return
    }
}
</script>

<template>
    <n-space :size="3" :wrap-item="false" vertical>
        <n-space :wrap-item="false" align="center" class="session-message">
            <template v-if="!props.command!.success">
                <div>{{ props.command!.output }}</div>
            </template>
            <template v-if="props.command!.success">
                <a :href="props.command!.output">{{ props.command!.output }}</a>
            </template>
        </n-space>
        <n-space :wrap="false" :wrap-item="false" align="center">
            <n-text :disabled="!props.command!.success" class="desc-button pointer" type="info" @click="open_link(props.command!.output)">打开</n-text>
            <n-text :disabled="!props.command!.success" class="desc-button pointer" type="info" @click="copy_to_clipboard(props.command!.output)">复制</n-text>
        </n-space>
    </n-space>
</template>

<style lang="scss" scoped></style>
